<?php
 	session_start();
	
	$check = array();
	$check['writeable'] = '';				// list of files and folder that must not be writeable
	$check['writeable'] = 'packingslips';   // list of files and folders that must be writeable
	$check['version'] = '5';				// minimum required PHP Version
	
	// general checks
		// paths
	
	// general Settings
	
	if(isset($_GET) && isset($_GET['check'])) {
		require_once('../classes/class.json.php');
		$json = new Services_JSON();
		
		switch($_GET['check']) {
			case "checkServer":
				$return = array(
					'PHP' => PHP_VERSION, 
					'configfile' => is_writable(dirname(dirname(__FILE__))."/config.inc.php")
				);
				echo $json->encode($return);
				break;
			
			case "DB":
				if(isset($_GET['set'])) {
					switch($_GET['set']) {
						case 'connect':
							// expect username, password, host and dbname
							
							
							
							if(!@mysql_connect($_GET['dbhost'],$_GET['dbuser'],$_GET['dbpass'])) {
								die(mysql_error());
							}
							$_SESSION['dbhost'] = $_GET['dbhost'];
							$_SESSION['dbuser'] = $_GET['dbuser'];
							$_SESSION['dbpass'] = $_GET['dbpass'];
							break;
							
						case "listDB":
						
							if(!@mysql_connect($_SESSION['dbhost'],$_SESSION['dbuser'],$_SESSION['dbpass'])) {
								die(mysql_error());
							}
							$dbs = mysql_list_dbs();
							$prohibit = array('mysql','information_schema','phpmyadmin');
							while ($row = @mysql_fetch_object($dbs)) {
								if (!in_array($row->Database,$prohibit)) { $list[] = $row->Database; }
							}
							$json = new Services_JSON();
							echo $json->encode($list);
							
							break;
							
						case 'selectdb':
							$_SESSION['dbname'] = $_GET['dbname'];
							break;
							
						case 'letsGo':
						
							mysql_connect($_SESSION['dbhost'],$_SESSION['dbuser'],$_SESSION['dbpass']);
							mysql_select_db($_SESSION['dbname']);
							
							ob_start();
								readfile(dirname(__FILE__)."/structure.sql");
								$sql = ob_get_contents();
							ob_end_clean();
							
							$queries = explode(";",$sql);
							foreach($queries as $q) {
								if($q != '') { mysql_query(trim($q)); }
								if($err = mysql_error()) die($err+"\n\n >".$q."<");
							}
							//
							break;
					}
				}
				break;
		}
	}
?>
